import unittest
import requests

import config
from api.RegLoginApi import RegLoginApi
from api.AccountApi import OpenAccountApi
from common import utils_p2p
from common.utils_p2p import common_assert, html_util


class TestOpenAccount(unittest.TestCase):
    # 类属性
    account_api = None
    session = requests.Session()

    @classmethod
    def setUp(cls) -> None:
        print("类前置，实例化对象")
        cls.login_api = RegLoginApi(cls.session)
        cls.account_api = OpenAccountApi(cls.session)

    @classmethod
    def tearDown(cls) -> None:
        print('类后置,关闭session对象')
        cls.session.close()

    def test01_user_register(self):
        self.login_api.user_login(keywords_v="13422221111", password_v="123abc")
        resp = self.account_api.realname("张三", "632323190605260500")
        # 断言
        utils_p2p.common_assert(self, resp, 200, 200, "提交成功")
        self.assertEqual("632****500", resp.json().get("data").get("card_id"))
        self.assertEqual("张**", resp.json().get("data").get("realname"))

    def test02_open_account(self):
        self.login_api.user_login(keywords_v='13422221115', password_v='123abc')
        # 实现开户接口
        response = self.account_api.open_account()
        config.account_data = response
        # 断言
        common_assert(self, response, 200, 200, 'form')

    # 第三方开户测试用例
    def test03_third_open_account(self):
        # 完成开户接口，获取response对象
        data = config.account_data
        cnd = html_util(data)
        # 调用封装获取第三方接口请求数据的方法，获取第三方开户接口的url
        url = cnd[0]
        # 调用封装获取第三方接口请求数据的方法，获取第三方开户接口的请求体数据
        info = cnd[1]
        # 调用封装的第三方开户接口的方法，获取第三方开户接口返回的响应结果
        response = self.account_api.third_open_account(url, info)
        # 断言
        self.assertEqual(200, response.status_code)
        self.assertIn("OK", response.text)
